﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BullsAndCows
{
    class ScoreBoard
    {
        int top;
        int[] score;
        string[] name;

        void OneStepSort()
        {
            for (int j = score.Length - 1; j > 0; j--)
            {
                if (score[j] > -1 && ( (score[j] < score[j - 1]) || (score[j - 1] == -1) ))
                {
                    int c = score[j - 1];
                    score[j - 1] = score[j];
                    score[j] = c;
                    string tmpName = name[j - 1];
                    name[j - 1] = name[j];
                    name[j] = tmpName;
                }
            }
        }

        public ScoreBoard(int top = 5)
        {
            this.top = top;
            score = new int[this.top];
            for (int i = 0; i < score.Length; i++)
                score[i] = -1;
            name = new string[this.top];
        }

        public void Print()
        {
            Console.WriteLine("\nScoreBoard");
            Console.WriteLine("--------------");
            for (int i = 0; i < score.Length; i++)
            {
                if (score[i] > -1)
                {
                    Console.WriteLine("Attempts: {0} , Name: {1}", score[i], name[i]);
                }
                else
                {
                    break;
                }
            }
            Console.WriteLine("--------------\n");
        }

        public void AddNew(int number, int hellperAttempts)
        {
            if (hellperAttempts == 0)
            {
                if ((number < score[score.Length - 1] || score[score.Length - 1] == -1))
                {
                    Console.Write("Enter Your Name: ");
                    string name = Console.ReadLine();
                    score[score.Length - 1] = number;
                    this.name[score.Length - 1] = name;
                    OneStepSort();
                }
            }
            else
            {
                Console.WriteLine("You are not allowed to enter the top scoreboard.");
            }
        }
    }
}
